package com.jiangyg.mall.core;

import com.jiangyg.mall.core.support.env.EnvChangeEventLister;
import com.jiangyg.mall.core.support.cache.CacheAdapter;
import com.jiangyg.mall.core.support.snowflake.SnowflakeBuilder;
import com.jiangyg.mall.core.utils.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * 类描述：核心基础配置
 *
 * @author jiangyg
 * @version 4.0
 * @date 2021-10-12
 */
@Slf4j
@Order(12)
@Configuration
public class CoreAutoConfiguration implements InitializingBean {

    /**
     * 功能描述：注册系统配置变化事件监听，用于实现动态配置
     *
     * @return 事件监听
     */
    @Bean
    public EnvChangeEventLister envChangeEventLister() {
        return new EnvChangeEventLister();
    }

    /**
     * 功能描述：初始化雪花ID生成器
     *
     * @param cacheAdapter 缓存
     * @return 雪花ID生成器
     */
    @Bean
    @ConditionalOnProperty(name = "snowflake.enable", havingValue = "true")
    public SnowflakeBuilder snowflakeBuilder(CacheAdapter cacheAdapter) {
        return SnowflakeBuilder.init(cacheAdapter);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Logger.info(log, () -> "[项目启动]-初始化核心配置……");
    }

}
